#################################################################################
#	REPLICATION SCRIPT								
#	When Marriage Gets Hard: Intra-Coalition Conflict and Electoral Accountability
# Carolina Plescia (carolina.plescia@univie.ac.at)					
#	THIS VERSION: 14-04-2021							
#											                              	                          #
#	NOTE:	This script allows to reproduce all numerical results and figures
#       presented in the main article.
#################################################################################




#Remember to set local working directory
setwd("~/replication files")

#Load required packages
library(rms)
library(texreg)
library(interplot)
library(interactions)
library(tidyverse)
library(dplyr)
library(jtools)


#Load required datasets
load("data.RData")

# All variables, except for binary variables, 
# are standardised by centring and dividing by two standard deviations (Gelman 2008)

"Negative economic perceptions"
data$eco_retro_R<-rescale(data$eco_retro, "full")
summary(data$eco_retro_R)

"Intra-coalition conflict"
data$mean_conflict_REV_R<-rescale(data$mean_conflict_REV, "full")
summary(data$mean_conflict_REV_R)

"Political sophistication"
data$pol_sophi_R<-rescale(data$pol_sophi, "full")
summary(data$pol_sophi_R)

"Days in governemnt"
data$days_int_elec_R<-rescale(data$days_int_elec, "full")
summary(data$days_int_elec_R)

"Powell-Whitten index"
data$PWindex_R<-rescale(data$PWindex, "full")
summary(data$PWindex_R)

"Institutional clarity index"
data$Insti_clarity_R<-rescale(data$Insti_clarity, "full")
summary(data$Insti_clarity_R)

"Government clarity index"
data$Gov_clarity_R<-rescale(data$Gov_clarity, "full")
summary(data$Gov_clarity_R)

"Age"
data$age_R<-rescale(data$age, "full")
summary(data$age_R)

#Correlation checks

cor(data$PWindex_R,data$Insti_clarity_R, use="complete.obs")
cor(data$PWindex_R,data$Gov_clarity_R, use="complete.obs")
cor(data$Insti_clarity_R,data$Gov_clarity_R, use="complete.obs")

cor(data$mean_conflict_REV_R, data$PWindex_R, use="complete.obs")
cor(data$mean_conflict_REV_R, data$Gov_clarity_R, use="complete.obs")
cor(data$mean_conflict_REV_R, data$Insti_clarity_R, use="complete.obs")

cor(data$mean_conflict_REV_R, data$earlytermination,use="complete.obs")
cor(data$mean_conflict_REV_R, data$minresignation,use="complete.obs")
cor(data$mean_conflict_REV_R, data$PMresignation,use="complete.obs")
cor(data$mean_conflict_REV_R, data$reshuffle,use="complete.obs")

data$PID_3cat.f <- factor(data$PID_3cat)
is.factor(data$PID_3cat.f)

data$cat_responsability.f <- factor(data$cat_responsability)
is.factor(data$cat_responsability.f)

data$cat_responsability.PM <- factor(data$cat_responsability_PM)
is.factor(data$cat_responsability.PM)

data$cat_responsability.J <- factor(data$cat_responsability_J)
is.factor(data$cat_responsability.J)



#############
# FIGURE 1	#
#############

dataforplot<-data[!(data$countryNAME=="Spain" | data$countryNAME=="Norway" | data$countryNAME=="Slovak Republic"),]
table(dataforplot$countryNAME)

data_figure1 = unique.data.frame(dataforplot[c("YEAR","countryNAME","mean_conflict_REV_R")])

ggplot(data_figure1,aes(y=mean_conflict_REV_R, x=countryNAME)) +
  facet_grid(~YEAR) +
  coord_flip() +
  ylab("Intra-coalition conflict") +
  xlab(" ") +
  geom_bar(stat='identity', size=20) +
  theme(axis.text=element_text(size=11),
         axis.title=element_text(size=14),strip.text.x = element_text(size = 11))

dev.copy(pdf, "Figure1.pdf", width=7.0, height=6.0)
dev.off()


#############
#  TABLE 1	#
#############

ECO_modelGOV=glmer(gov_vote ~ 1+eco_retro_R+mean_conflict_REV_R+
                     PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                     pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

ECO_modelGOV.I=glmer(gov_vote ~ 1+eco_retro_R*mean_conflict_REV_R+
                       PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                       pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

ECO_modelPM=glmer(PM_vote ~ 1+eco_retro_R+mean_conflict_REV_R+
                    PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                    pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

ECO_modelPM.I=glmer(PM_vote ~ 1+eco_retro_R*mean_conflict_REV_R+
                      PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                      pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

ECO_modelJ=glmer(juniorPart_vote ~ 1+eco_retro_R+mean_conflict_REV_R+
                   PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                   pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

ECO_modelJ.I=glmer(juniorPart_vote ~ 1+eco_retro_R*mean_conflict_REV_R+
                     PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                     pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

screenreg(list(ECO_modelGOV,ECO_modelGOV.I, ECO_modelPM,ECO_modelPM.I,
               ECO_modelJ, ECO_modelJ.I), booktabs = TRUE, dcolumn = TRUE,digits=3)

htmlreg(list(ECO_modelGOV,ECO_modelGOV.I, ECO_modelPM,ECO_modelPM.I,
             ECO_modelJ, ECO_modelJ.I),
        custom.model.names = c("GOVERNMENT", "GOVERNMENT",
                               "PM", "PM",
                               "JUNIOR", "JUNIOR"),
        custom.coef.names = c("(Intercept)", "Negative economic perceptions", "Intra-government conflict",
                              "PM partisan", "Junior partisan","Opposition partisan", "Age",
                              "Female","Days in government", "Powell-Whitten index",  "Institutional clarity index",
                              "Government clarity index", "Political sophistication", "Perceptions x conflict"),
        reorder.coef = c(2:3, 14, 4:6, 9:12,  7:8, 13, 1), booktabs = TRUE, dcolumn = TRUE,
        groups = list("Controls" = 4:10, "Demographics" = 11:13), 
        file = "Table1.doc",digits=3)



#############
# FIGURE 2	#
#############

interplot(m = ECO_modelGOV.I,var1 = "eco_retro_R", var2 = "mean_conflict_REV_R")+
  xlab("Intra-coalition conflict (standardized)") +
  ylab("Marginal effect of negative perceptions on vote choice") +
  theme(plot.title = element_text(hjust = 0.5, face="bold")) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  theme(axis.text=element_text(size=14), axis.title=element_text(size=14)) +
  scale_y_continuous(limits=c(-1.75, 0.5), breaks = seq(-1.75, 0.5, by = 0.25)) +
  scale_x_continuous(limits=c(-1, 1.01), breaks = seq(-1, 1.01, by = 0.50))

dev.copy(pdf, "Figure2.pdf", width=7.0, height=6.0)
dev.off()



#############
# FIGURE 3a	#
#############

interplot(m = ECO_modelPM.I,var1 = "eco_retro_R", var2 = "mean_conflict_REV_R")+
  xlab("Intra-coalition conflict (standardized)") +
  ylab("Marginal effect of negative perceptions on vote choice (PM)") +
  theme(plot.title = element_text(hjust = 0.5, face="bold")) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  theme(axis.text=element_text(size=14), axis.title=element_text(size=14)) +
  scale_y_continuous(limits=c(-1.75, 0.5), breaks = seq(-1.75, 0.5, by = 0.25))+
  scale_x_continuous(limits=c(-1, 1.01), breaks = seq(-1, 1.01, by = 0.50))

dev.copy(pdf, "Figure3a.pdf", width=7.0, height=6.0)
dev.off()


#############
# FIGURE 3b	#
#############


interplot(m = ECO_modelJ.I,var1 = "eco_retro_R", var2 = "mean_conflict_REV_R")+
  xlab("Intra-coalition conflict (standardized)") +
  ylab("Marginal effect of negative perceptions on vote choice (Junior)") +
  theme(plot.title = element_text(hjust = 0.5, face="bold")) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  theme(axis.text=element_text(size=14), axis.title=element_text(size=14)) +
  scale_y_continuous(limits=c(-1.75, 0.5), breaks = seq(-1.75, 0.5, by = 0.25))+
  scale_x_continuous(limits=c(-1, 1.01), breaks = seq(-1, 1.01, by = 0.50))

dev.copy(pdf, "Figure3b.pdf", width=7.0, height=6.0)
dev.off()





###############################
# models for Figures 4 and 5	#
###############################

ECO_modelPM.w=glmer(PM_vote ~ 1+eco_retro_R+mean_conflict_REV_R+cat_responsability_PM+
                      PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                      pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

ECO_modelPM.3=glmer(PM_vote ~ 1+eco_retro_R*mean_conflict_REV_R*cat_responsability_PM+
                      PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                      pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

ECO_modelJ.w=glmer(juniorPart_vote ~ 1+eco_retro_R+mean_conflict_REV_R+cat_responsability_J+
                     PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                     pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))

ECO_modelJ.3=glmer(juniorPart_vote ~ 1+eco_retro_R*mean_conflict_REV_R*cat_responsability_J+
                     PID_3cat.f+age_R+female +PWindex_R+Insti_clarity_R+Gov_clarity_R+days_int_elec_R+
                     pol_sophi_R+(1|countrycode)+(1|countryNAME), data=data, family=binomial(link="logit"), control=glmerControl(optimizer="bobyqa"))


#############
# FIGURE 4	#
#############


interact_plot(ECO_modelPM.3, pred =eco_retro_R, modx = mean_conflict_REV_R, mod2 = cat_responsability_PM,
              mod2.labels=c("shared responsibility", "PM responsibility"),
              legend.main = "Intra-coalition conflict (standardized)",
              colors = c("grey80", "grey50", "black"), 
              y.label = "Voting for the PM party",
              x.label = "Negative economic perceptions (standardized)")

dev.copy(pdf, "Figure4.pdf", width=7.5, height=5.0)
dev.off()



#############
# FIGURE 5	#
#############

interact_plot(ECO_modelJ.3, pred =eco_retro_R, modx = mean_conflict_REV_R, mod2 = cat_responsability_J,
              mod2.labels=c("shared responsibility", "Junior responsibility"),
              legend.main = "Intra-coalition conflict (standardized)",
              colors = c("grey80", "grey50", "black"), 
              y.label = "Voting for the Junior party",
              x.label = "Negative economic perceptions (standardized)")

dev.copy(pdf, "Figure5.pdf", width=7.5, height=5.0)
dev.off()

